package AIGUI;
//理解图片界面
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.image.Image2TextResponse;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Base64;

 class UDSPICGUI {
    public  void createAndShowGUI() {
        // 直接在代码中设置访问密钥和密钥
        String accessKey = "ALTAKtcDh0nQtP40NgZUAmDA4K";
        String secretKey = "57a949833c7b408a81e06959785d90cf";

        // 创建 Qianfan 实例
        Qianfan qianfan = new Qianfan(accessKey, secretKey);

        // 创建主窗口
        JFrame frame = new JFrame("图片理解");
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout());

        // 创建顶部面板用于输入图片链接和生成按钮
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new FlowLayout());

        JLabel promptLabel = new JLabel("请输入图片链接:");
        JTextField promptField = new JTextField(30);
        JButton generateButton = new JButton("生成");

        inputPanel.add(promptLabel);
        inputPanel.add(promptField);
        inputPanel.add(generateButton);

        frame.add(inputPanel, BorderLayout.NORTH);

        // 创建中央面板用于显示图片
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());

        JLabel imageLabel = new JLabel();
        JScrollPane imageScrollPane = new JScrollPane(imageLabel);
        centerPanel.add(imageScrollPane, BorderLayout.CENTER);

        frame.add(centerPanel, BorderLayout.CENTER);

        // 创建底部面板用于显示理解的文本内容
        JTextArea resultArea = new JTextArea(10, 50);
        resultArea.setEditable(false);
        resultArea.setLineWrap(true);
        resultArea.setWrapStyleWord(true);
        resultArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        JScrollPane resultScrollPane = new JScrollPane(resultArea);
        frame.add(resultScrollPane, BorderLayout.SOUTH);

        // 添加生成按钮的事件监听器
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String imageUrl = promptField.getText();
                String base64Image = convertImageToBase64(imageUrl);

                if (base64Image != null) {
                    try {
                        Image2TextResponse response = qianfan.image2Text()
                                .image(base64Image)
                                .prompt("introduce the picture")
                                .execute();

                        String resultText = response.getResult();
                        System.out.println("Generated Text: " + resultText); // 调试输出生成的文本
                        resultArea.setText(resultText);

                        // 显示图片
                        byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                        ImageIcon imageIcon = new ImageIcon(imageBytes);
                        imageLabel.setIcon(imageIcon);

                    } catch (Exception ex) {
                        ex.printStackTrace();
                        resultArea.setText("Failed to get image understanding. Error: " + ex.getMessage());
                    }
                } else {
                    resultArea.setText("Failed to convert image to Base64.");
                }
            }
        });

        // 显示窗口
        frame.setVisible(true);
    }

    private static String convertImageToBase64(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            is.close();
            byte[] imageBytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
